package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.DBHelper;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository repository;

    @Autowired
    private IRegionService regionService;

    @Autowired
    private MongoTemplate template;

    @Override
    public void save(Destination destination) {
        destination.setId(null);
        repository.save(destination);
    }

    @Override
    public void saveOrUpdate(Destination destination) {
        repository.save(destination);
    }

    @Override
    public void dilete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return repository.findAll();
    }

    //分页
    @Override
    public Page<Destination> query(DestinationQuery qo) {
        Query query = new Query();
        if (StringUtils.hasLength(qo.getKeyword())) {
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }

        if (StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.parentId));
        }else {
            query.addCriteria(Criteria.where("parentId").is(null));
        }

        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template, query, qo, Destination.class, pageable);
    }


    @Override
    public List<Destination> queryByToasts(String destId) {
        ArrayList<Destination> list = new ArrayList<>();
        this.createToasts(list, destId);
        //颠倒list中的元素顺序达到子在爹的后面的效果
        Collections.reverse(list);
        return list;
    }

    private void createToasts(List<Destination> list, String id) {
        if (!StringUtils.hasLength(id)) {
            return;
        }
        Destination dest = this.get(id);
        list.add(dest);
        if (StringUtils.hasLength(dest.getParentId())) {
            this.createToasts(list, dest.getParentId());
        }
    }

    @Override
    public List<Destination> queryDestByRegionIdForApi(String regionId) {

        List<Destination> list = new ArrayList<>();
        if ("-1".equals(regionId)){
            list = repository.findByParentName("中国");
        }else {
            list = this.queryDestByRegionId(regionId);
        }
        for (Destination destination : list) {
            Pageable pageable = PageRequest.of(0, 5, Sort.Direction.ASC, "_id");
            List<Destination> children = repository.findByParentId(destination.getId(),pageable);
            destination.setChildren(children);
        }
        return list;
    }

    @Override
    public List<Destination> queryDestByRegionId(String rid) {
        Region region = regionService.get(rid);
        return repository.findByIdIn(region.getRefIds());
    }

    @Override
    public Destination getByName(String keyword) {
        return repository.findByName(keyword);
    }
}
